﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Facade.Manager.Abstract;

using Domain.Model;

using DataAccess.Repositories;

namespace Facade.Manager
{
    public class WebFormManager : AbstractManager
    {
        private static readonly WebFormManager instance = new WebFormManager();

        private FormRepository webFormRepository;

        public Form WebForm;

        static WebFormManager()
        {}

        public WebFormManager()
        {
            webFormRepository = new FormRepository();

            WebForm = webFormRepository.GetById(1);

            if (WebForm == null)
            {
                WebForm = new Form();
            }
        }

        public static WebFormManager Instance
        {
            get 
            {
                return instance;
            }
        }

        public void AddFormElement(string name, string type)
        {
            int position = WebForm.FormElements.Count;
            var element = new FormElement(name, position, type);

            WebForm.AddFormElement(element);

            UpdateWebForm();
        }

        public void DeleteFormElementById(int id)
        {
            FormElement element = WebForm.GetElementById(id);

            if (element != null)
            {
                webFormRepository.DeleteWebFormElement(element);

                WebForm.RemoveFormElement(element);

                UpdateWebForm();
            }
        }

        private void UpdateWebForm()
        {
            webFormRepository.Update(WebForm);
        }
    }
}